#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;

#define SIZE 20

int main()
{
	int i, j;
    char floor[SIZE][SIZE];
    fill_n ( (char*)floor, SIZE*SIZE, ' ');

    char *buffer = new char[20];
    int direction = 1;
    char star = 'x';

    struct 
    {
        int x;
        int y;
    }current;

    current.x = 0;
    current.y = 0;

    while (1)
    {
        cout << "Enter command (9 to end input): ";
        fgets(buffer, 20, stdin);
        if (strchr (buffer, ',') == NULL)
        {
            //other commands
            int cmd, rt;
            rt = sscanf (buffer, "%d", &cmd);
            if (rt != 1)
            {
                cout << "syntax error\n";
                continue;
            }

            switch (cmd)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                    direction = cmd;
                    break;
                case 6:
                    cout << "\nThe drawing is\n" << endl;
                    for (i = 0; i < SIZE; i++)
                    {
                        for (j = 0; j < SIZE; j++)
                            cout << floor[i][j];
                        cout << endl;
                    }
                    break;
                case 9:
                    exit (EXIT_SUCCESS);
                    break;
                default:
                    cout << "invalid command\n";
                    break;
            }
        } else {
            //move commands
            int cmd, spaces, rt;
            rt = sscanf (buffer, "%d,%d", &cmd, &spaces);
            if (rt != 2) 
            {
                cout << "syntax error\n";
                continue;
            }
            switch (cmd)
            {
                case 5:
                    for (i = 0; i < spaces; i++)
                    {
                        switch (direction) {
                            case 4: //up
                                if (current.y == 0)
                                    goto wall;
                                else 
                                {
                                    floor[current.x][current.y] = star;
                                    current.y--;
                                }
                                break;
                            case 3: //down
                                if (current.y == SIZE - 1)
                                    goto wall;
                                else
                                {
                                    floor[current.x][current.y] = star;
                                    current.y++;
                                }
                                break;
                            case 2: //right
                                if (current.x == SIZE - 1) 
                                    goto wall;
                                else 
                                {
                                    floor[current.x][current.y] = star;
                                    current.x++;
                                }
                                break;
                            case 1: //left
                                if (current.x == 0)
                                    goto wall;
                                else 
                                {
                                    floor[current.x][current.y] = star;
                                    current.x--;
                                }
                                break;
                        }
                    }
                    break;
                default:
                    cout << "invalid command\n";
                    continue;
            }
            continue;

            wall:
                cout << "HIT THE WALL!\n";
        }
        
    }

    delete[] buffer;

    return 0;
}
